# -*- coding: utf-8 -*-

from odoo import models, fields, api


class CpqCurrency(models.Model):
    _name = 'cpq.currency'
    _description = '货币'
    _rec_name = 'name'
    _order = 'name'

    name = fields.Char('货币名称', required=True, index=True, help='如：人民币、美元')
    symbol = fields.Char('货币符号', required=True, help='如：¥、$、€')
    code = fields.Char('货币代码', required=True, index=True, help='如：CNY、USD、EUR')
    
    # 汇率（相对于基准货币）
    rate = fields.Float('汇率', default=1.0, digits=(16, 6), help='相对于基准货币的汇率')
    is_base = fields.Boolean('基准货币', default=False, help='是否为基准货币')
    
    # 精度
    decimal_places = fields.Integer('小数位数', default=2, help='金额显示的小数位数')
    
    active = fields.Boolean('有效', default=True)
    notes = fields.Text('备注')
    
    @api.model
    def get_base_currency(self):
        """获取基准货币"""
        base = self.search([('is_base', '=', True)], limit=1)
        if not base:
            # 如果没有设置基准货币，返回第一个货币
            base = self.search([], limit=1)
        return base
    
    def name_get(self):
        result = []
        for record in self:
            name = f'{record.symbol} {record.name} ({record.code})'
            result.append((record.id, name))
        return result

